package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Setmeal;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.sevrice.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        IPage<SetmealDto> page = setmealService.findPage(pageParam);
        return R.success(page);
    }

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R addSetmeal(@RequestBody SetmealDto setmealDto){
        int row = setmealService.addSetmeal(setmealDto);
        if (row>0){
            return  R.success("添加套餐成功！");
        }
        return R.error("添加套餐失败");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteSetmeal(@RequestParam List<Long> ids){
        int row = setmealService.deleteSetmeal(ids);
        if (row>0){
            return R.success("删除套餐成功！");
        }
        return R.error("删除套餐失败~！");
    }

    /**
     * 更新套餐的状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R updateType(@PathVariable Integer status, @RequestParam List<Long> ids){
        int row = setmealService.updateType(status, ids);
        if (row>0){
            return R.success("修改套餐状态成功！");
        }
        return R.error("修改套餐状态失败~！");
    }

    /**
     * 根据套餐的Id 查询套餐菜品。
     * @param setmealId
     * @return
     */
    @GetMapping("/{setmealId}")
    public R findById(@PathVariable Long setmealId){
        SetmealDto dto = setmealService.findById(setmealId);
        return R.success(dto);
    }


    /**
     * 更新套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto){
        int row = setmealService.updateSetmeal(setmealDto);
        if (row>0){
            return  R.success("修改套餐成功");
        }
        return R.error("修改套餐失败");
    }

    /**
     * 根据分类Id  查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R findByCategoryId(Setmeal setmeal){
        List<SetmealDto> setmealList = setmealService.findByCategoryId(setmeal);
        return R.success(setmealList);
    }

    /**
     * 移动端查看套餐信息
     * @param setmealId
     * @return
     */
    @GetMapping("/dish/{setmealId}")
    public R findDish(@PathVariable Long setmealId){
        List<DishDto> dish = setmealService.findDish(setmealId);
        return R.success(dish);
    }


}
